<template>
	<view>
		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel" class="framebzwork">

			<template #content>
				<view class="content_pagingBox">

					<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" class="z_paging"
						:loading-more-enabled="ShowLoadingmore" :style="{ height: scrollLiftHeight + 'px'}">

						<template #top>
							<view class="z-paging_top">
								<view class="z-paging_top_child" @click="cellClick('返回')">
									<u-icon name="arrow-left"></u-icon>
									<text>返回</text>
								</view>
								<u-search placeholder="搜索" v-model="searchKeyword" @search="handleSearch"
									@custom='handleSearch'></u-search>
							</view>
						</template>
						<view class="" v-for="(item,index) in dataList" :key="index">
							<u-cell :title="item.name" @click="cellClick(item)"></u-cell>
						</view>
					</z-paging>
				</view>
			</template>
		</st-mc-framework>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDate,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';
	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				ModuleTitle: '',
				showMLoading: false,
				loadingName: '正在加载中……',
				searchKeyword: '',
				pageParam: {},
				initData: {},
				initObj: {},
				dataList: [],
				filteredList: [],
				scrollLiftHeight: uni.getSystemInfoSync(), //列表高度
				searchShow: true,
				isPaging: true,
				ShowLoadingmore: true, // 加载完底部显示文本
			};
		},
		onLoad(onlod) {
			let that = this;
			that.ModuleTitle = that.$store.state.appConfigInfo.ActiveModule.ModuleName
			that.showMLoading = true
			if (!onlod.obj) {
				that.showMLoading = false
				return
			}
			var obj = JSON.parse(onlod.obj)
			that.pageParam = obj
			// 是否分页
			if (that.pageParam.serverSearch.hasOwnProperty('isPaging')) {
				this.isPaging = that.pageParam.serverSearch.isPaging;
				this.ShowLoadingmore = that.pageParam.serverSearch.isPaging;
			}

			// that.searchShow = that.pageParam.serverSearch.searchShow
			that.$nextTick(() => {
				uni.createSelectorQuery().select('.content_pagingBox').boundingClientRect(res => {
					that.scrollLiftHeight = res.height;
				}).exec()
			});

		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {

			handleSearch() {
				let that = this;

				if (that.pageParam.serverSearch.searchShow) {
					// 获取searchKey指定的参数名
					const searchParamKey = that.pageParam.serverSearch.searchKey;

					// 将searchKeyword赋值给param中对应的属性
					that.pageParam.param[searchParamKey] = that.searchKeyword;

					that.contents = []; // 假设这是异步获取数据的操作
					that.$refs.paging.reload();
				}


			},
			cellClick(item) {
				if (item != '返回') {
					item.type = this.pageParam.type
				}
				
				uni.$emit('refreshDetail', {
					data: item
				});
				uni.navigateBack({
					delta: 1
				})
			},
			queryList(pageNo) {
				let that = this;
				var GetArray = 'ds'
				if (!that.pageParam.cmd) {
					return
				}
				if (that.pageParam.serverSearch.arrayList) {
					GetArray = that.pageParam.serverSearch.arrayList;
				}
				if (that.pageParam.serverSearch.isPaging) {
					var pageIndexStr = that.pageParam.serverSearch.pageIndexStr || 'PageIndex';
					var pageSizeStr = that.pageParam.serverSearch.pageSizeStr || 'PageSize';
					that.pageParam.param[pageIndexStr] = pageNo
				}


				try {
					that.Request.request(this.pageParam.cmd, this.pageParam.param, function(ret) {
						console.log(ret);
						const showTextKey = that.pageParam.serverSearch.showText;
						if (ret.status == 200) {
							// 根据GetArray获取数据
							const Displayarray = GetArray === 'ds' ? ret.data.ds : ret.data[GetArray];
							that.showMLoading = false
							var item
							if (that.pageParam.type == '机台') {
								item = {
									EquipmentID: 0,
									EquipmentName: "全部",
									EquipmentCode: "全部",
									name: '全部',
									FWorkshopID: null
								}
								Displayarray.unshift(item)
								Displayarray.map(item => item.name = item.EquipmentName)
							} else {
								Displayarray.map(item => {
									item.name = item[showTextKey]; // 自动选择 ProductPJName 或 ProtPJName
								});
							}
							if (that.isPaging) {
								if (Displayarray.length == 0) {
									that.$refs.paging.complete();
									that.$utils.hideLoading()
									return
								}
								that.$refs.paging.complete(Displayarray);
								that.$utils.hideLoading();
							} else {
								let TheData = JSON.parse(JSON.stringify(Displayarray))
								that.$refs.paging.complete(TheData);
								that.$utils.hideLoading();
								setTimeout(() => {
									that.$refs.paging.endRefresh()
								}, 100);
							}

						} else {
							that.showMLoading = false
						}
					}, function(err) {
						that.showMLoading = false
						console.log(JSON.stringify(err))
						that.$refs.paging.complete(false);
						that.$utils.hideLoading()
					})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}

			},
			changeLevel(list, data, row) {
				getchangeLevel(data)
				uni.$emit('refreshList');
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.content_pagingBox {
		width: 100%;
		height: 100%;
	}

	.z-paging_top {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 1%;
		box-sizing: border-box;

		.z-paging_top_child {
			display: flex;
			align-items: center;
			margin-right: 2%;
		}
	}
</style>